﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using MakeItWeb.Framework;

namespace AllNations.Framework.UserControls
{
    public partial class DatabaseContent : MakeItWebUserControl, IOrderable, IDynamicContent
    {
        public enum ToolFile
        {
            Simple,
            Full,
            SimpleWithPicture
        }

        MakeItWeb.Framework.DatabaseContent m_oDatabaseContent = null;
        string m_sContentEmptyMessage = string.Empty;
        ToolFile m_EditorMode = ToolFile.Full;
        Unit? m_EditorHeight = null;
        string m_sContentCd;
        bool m_bViewEditButton = true;
        bool m_bDisplayedInMultiSection = false;
        bool m_bCanMoveUp = false;
        bool m_bCanMoveDown = false;

        public string ContentCd
        {
            get { return m_sContentCd; }
            set { m_sContentCd = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string sCacheKey = string.Format("DatabaseContent_{0}", ContentCd);
            if (m_bDisplayedInMultiSection)
                sCacheKey = string.Format("{0}_{1}", sCacheKey, CurrentHtmlPage.HtmlPageId);

            m_oDatabaseContent = Cache.Get(sCacheKey) as MakeItWeb.Framework.DatabaseContent;
            if (m_oDatabaseContent == null)
            {
                if (m_bDisplayedInMultiSection)
                    m_oDatabaseContent = MakeItWeb.Framework.DatabaseContent.GetDatabaseContentByCd(ContentCd, CurrentHtmlPage.HtmlPageId);
                else
                    m_oDatabaseContent = MakeItWeb.Framework.DatabaseContent.GetDatabaseContentByCd(ContentCd);
                Cache.Insert(sCacheKey, m_oDatabaseContent);
            }
        }

        protected void menuAdmin_ItemClick(object sender, RadMenuEventArgs e)
        {
            switch (e.Item.Value)
            {
                case "EditSectionContent":
                    Response.Redirect(string.Format("/WebAppAdmin/EditContent.aspx?ContentCd={0}&HtmlPageId={1}", ContentCd, CurrentHtmlPage.HtmlPageId), true);
                    break;
                case "MoveUpSection":
                    MakeItWeb.Framework.DatabaseContent.MoveUpDatabaseContentByCd(ContentCd);
                    Response.Redirect(HtmlPage.GetRawUrl(), true);
                    break;
                case "MoveDownSection":
                    MakeItWeb.Framework.DatabaseContent.MoveDownDatabaseContentByCd(ContentCd);
                    Response.Redirect(HtmlPage.GetRawUrl(), true);
                    break;
                case "DeleteSection":
                    MakeItWeb.Framework.DatabaseContent.DeleteDatabaseContentByCd(ContentCd);
                    Response.Redirect(HtmlPage.GetRawUrl(), true);
                    break;
                case "ExportSection":
                    if (!string.IsNullOrEmpty(m_oDatabaseContent.ContentHtmlTxt))
                    {
                        HttpContext.Current.Response.Clear();
                        HttpContext.Current.Response.AddHeader("Content-Length", m_oDatabaseContent.ContentHtmlTxt.Length.ToString());
                        HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.html", SiteMap.CurrentNode.Title));
                        HttpContext.Current.Response.ContentType = "text/html";
                        HttpContext.Current.Response.Write(m_oDatabaseContent.ContentHtmlTxt);
                        HttpContext.Current.Response.End();
                    }
                    break;
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            DatabaseContentArea.CssClass = IsPageAdministrator() ? "DatabaseContentAreaAdmin" : "DatabaseContentArea";

            if (string.IsNullOrEmpty(m_oDatabaseContent.ContentHtmlTxt))
                litContent.Text = m_sContentEmptyMessage;
            else
                litContent.Text = m_oDatabaseContent.ContentHtmlTxt;

            panelAdminMenu.Visible = IsPageAdministrator();
            if (panelAdminMenu.Visible)
            {
                RadMenuItem root = menuAdmin.FindItemByValue("AdminRoot");
                root.Items.FindItemByValue("EditSectionContent").Enabled = (IsPageAdministrator() && ViewEditButton);
                root.Items.FindItemByValue("DeleteSection").Enabled = (IsPageAdministrator() && DisplayedInMultiSection);
                root.Items.FindItemByValue("MoveUpSection").Enabled = (IsPageAdministrator() && DisplayedInMultiSection && m_bCanMoveUp);
                root.Items.FindItemByValue("MoveDownSection").Enabled = (IsPageAdministrator() && DisplayedInMultiSection && m_bCanMoveDown);
            }
            base.OnPreRender(e);
        }

        public bool ViewEditButton
        {
            get { return m_bViewEditButton; }
            set { m_bViewEditButton = value; }
        }
        public string ContentEmptyMessage
        {
            get { return m_sContentEmptyMessage; }
            set { m_sContentEmptyMessage = value; }
        }
        public ToolFile EditorMode
        {
            get { return m_EditorMode; }
            set { m_EditorMode = value; }
        }
        public Unit? EditorHeight
        {
            get { return m_EditorHeight.Value; }
            set { m_EditorHeight = value; }
        }
        public bool DisplayedInMultiSection
        {
            get { return m_bDisplayedInMultiSection; }
            set { m_bDisplayedInMultiSection = value; }
        }

        #region IOrderable Members

        bool IOrderable.CanMoveUp
        {
            get { return m_bCanMoveUp; }
            set { m_bCanMoveUp = value; }
        }

        bool IOrderable.CanMoveDown
        {
            get { return m_bCanMoveDown; }
            set { m_bCanMoveDown = value; }
        }

        #endregion

        #region IDynamicContent Members

        public string DynamicContent
        {
            get
            {
                return litContent.Text;
            }
            set
            {
                litContent.Text = value;
            }
        }

        #endregion
    }
}